package it.sportingnola.soccernotes;

import it.sportingnola.soccernotes.adapter.SubstAdapter;
import it.sportingnola.soccernotes.listener.TeamSubstItemListener;
import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class SubstEdit extends AbstractSoccerActivity {
    private Spinner player_inSpinner;
    private Spinner player_outSpinner;
    private Spinner teamSpinner;
    private EditText idSubstText;
    private EditText timeText;
    private Spinner halfSpinner;
    private ListView griglia;
    private TextView titleFormText;
    private final String[] fromList=new String[]{Const.Subst.KEY_TEAM, Const.Subst.KEY_NAME_PLAYER_IN,
	Const.Subst.KEY_NAME_PLAYER_OUT, Const.Subst.KEY_HALF, Const.Subst.KEY_MIN };
    private final int[] toList=new int[]{R.id.team_old, R.id.player_in_old, R.id.player_out_old, R.id.half_old,
	R.id.min_old };
    private final String[] fromPlSp=new String[]{Const.Players.KEY_NAME };
    private final int[] toPlSp=new int[]{android.R.id.text1 };

    @Override
    public void cancella(View view) {
	String rowId=idSubstText.getText().toString();
	if ( ! "".equals(rowId)) {
	    dbHelper.cancellaSubst(Long.parseLong(rowId));
	    Toast toast=Toast.makeText(this, getString(R.string.subst_deleted), Toast.LENGTH_SHORT);
	    toast.show();
	    fillData();
	}
	resetField();
    }

    public void createAndSetPlayerAdapter(int team) {
	Cursor playerInCur=dbHelper.getAllPlayers(cronaca, team, true, true);
	Cursor playerOutCur=dbHelper.getAllPlayers(cronaca, team, false, true);
	SimpleCursorAdapter playerInAdapter=new SimpleCursorAdapter(this,
	    android.R.layout.simple_spinner_dropdown_item, playerInCur, fromPlSp, toPlSp);
	SimpleCursorAdapter playerOutAdapter=new SimpleCursorAdapter(this,
	    android.R.layout.simple_spinner_dropdown_item, playerOutCur, fromPlSp, toPlSp);
	player_inSpinner.setAdapter(playerInAdapter);
	player_outSpinner.setAdapter(playerOutAdapter);
    }

    @Override
    public void doLeftSwipeAction() {
	editAction(null);
    }

    @Override
    public void doRigthSwipeAction() {
	editCard(null);

    }

    @Override
    protected void fillData() {
	Cursor sc=dbHelper.cercaSubst(cronaca);
	SubstAdapter substAdapter=new SubstAdapter(this, R.layout.subst_row, sc, fromList, toList);
	griglia.setAdapter(substAdapter);
    }

    public Spinner getPlayer_inSpinner() {
	return player_inSpinner;
    }

    public Spinner getPlayer_outSpinner() {
	return player_outSpinner;
    }

    @Override
    protected void performSetContentView() {
	setContentView(R.layout.subst_edit);
    }

    @Override
    protected void performSetTitle() {
	setTitle(getText(R.string.substitutions));
    }

    /**
     * Inizializza i campi presenti nella View
     */
    @Override
    protected void prepareFields() {
	titleFormText=(TextView) SubstEdit.this.findViewById(R.id.form_subst);
	idSubstText=(EditText) findViewById(R.id.id_subst);
	player_inSpinner=(Spinner) findViewById(R.id.player_in);
	player_outSpinner=(Spinner) findViewById(R.id.player_out);
	teamSpinner=(Spinner) findViewById(R.id.team_subst);
	ArrayAdapter<CharSequence> teamAdapter=new ArrayAdapter<CharSequence>(this,
	    android.R.layout.simple_spinner_dropdown_item);
	teamAdapter.add(team1Name);
	teamAdapter.add(team2Name);
	teamSpinner.setAdapter(teamAdapter);
	TeamSubstItemListener teamListener=new TeamSubstItemListener(this);
	teamSpinner.setOnItemSelectedListener(teamListener);
	halfSpinner=(Spinner) findViewById(R.id.half);
	ArrayAdapter<CharSequence> halfAdapter=ArrayAdapter.createFromResource(this, R.array.halfs,
	    android.R.layout.simple_spinner_dropdown_item);
	halfSpinner.setAdapter(halfAdapter);
	timeText=(EditText) findViewById(R.id.min);
	griglia=(ListView) findViewById(R.id.gridview);
	griglia.setOnItemClickListener(new OnItemClickListener() {

	    @Override
	    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		Cursor c=SubstEdit.this.dbHelper.recuperaSubst(id);
		if (c.moveToFirst()) {
		    SubstEdit.this.titleFormText.setText(getString(R.string.edit_subst));
		    SubstEdit.this.halfSpinner.setSelection(c.getInt(c.getColumnIndex(Const.Subst.KEY_HALF)));
		    SubstEdit.this.idSubstText.setText(Long.toString(id));
		    SubstEdit.this.timeText.setText(c.getString(c.getColumnIndex(Const.Subst.KEY_MIN)));
		    int actTeam=c.getInt(c.getColumnIndex(Const.Cards.KEY_TEAM));
		    int prevTeam=teamSpinner.getSelectedItemPosition();
		    if (prevTeam != actTeam) {
			// il false serve a far scattare subito l'OnItemSelectedListener associato
			// al teamSpinner e ricreare i player spinner
			SubstEdit.this.teamSpinner.setSelection(actTeam, false);
		    }
		    // HACK: se non aggiungo il true ho uno strano bug: "qualcuno" reimposta i due
		    // spinner a 0
		    SubstEdit.this.player_inSpinner.setSelection(Util.getPlayerPos(
			c.getLong(c.getColumnIndex(Const.Subst.KEY_PLAYER_IN)),
			SubstEdit.this.player_inSpinner.getAdapter()), true);
		    SubstEdit.this.player_outSpinner.setSelection(Util.getPlayerPos(
			c.getLong(c.getColumnIndex(Const.Subst.KEY_PLAYER_OUT)),
			SubstEdit.this.player_outSpinner.getAdapter()), true);
		}
	    }

	});
    }

    @Override
    protected void resetField() {
	SubstEdit.this.halfSpinner.setSelection(0);
	SubstEdit.this.teamSpinner.setSelection(0);
	SubstEdit.this.player_inSpinner.setSelection(0);
	SubstEdit.this.player_outSpinner.setSelection(0);
	SubstEdit.this.idSubstText.setText("");
	SubstEdit.this.timeText.setText("");
	titleFormText.setText(getString(R.string.new_subst));
    }

    @Override
    public void salva(View view) {
	saveState();
	resetField();
	fillData();
    }

    @Override
    protected void saveState() {
	long player_in=player_inSpinner.getSelectedItemId();
	long player_out=player_outSpinner.getSelectedItemId();
	String rowId=idSubstText.getText().toString();
	int team=teamSpinner.getSelectedItemPosition();
	int half=halfSpinner.getSelectedItemPosition();
	String min=timeText.getText().toString();
	if (rowId.equals("")) {
	    dbHelper.insertSubst(player_in, player_out, team, half, min, cronaca);
	}
	else {
	    dbHelper.updateSubst(Long.parseLong(rowId), player_in, player_out, team, half, min, cronaca);
	}
	Toast toast=Toast.makeText(this, getString(R.string.just_saved), Toast.LENGTH_SHORT);
	toast.show();
    }
}
